package com.app.aipass.fragment.main;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;

import com.app.aipass.data.DemoRepository;
import com.app.aipass.entity.OtcConfig;
import com.app.aipass.entity.TimeNumZone;
import com.app.aipass.entity.WalletEntity;

import java.util.HashMap;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.http.BaseResponse;
import me.goldze.mvvmhabit.utils.RxUtils;

public class TransactionViewModel extends BaseViewModel<DemoRepository>
{
    public SingleLiveEvent<OtcConfig> mOtcConfig = new SingleLiveEvent<>();

    public TransactionViewModel(@NonNull Application application, DemoRepository repository)
    {
        super(application, repository);
    }

    /**
     * 网络请求方法，在ViewModel中调用Model层，通过Okhttp+Retrofit+RxJava发起请求
     */
    public void requestNetWork() {
        Log.e("TAG","==========requestNetWork======otcConfig=======");
        //可以调用addSubscribe()添加Disposable，请求与View周期同步
        HashMap<String, Object> map = new HashMap<>();
//        lang=zh-cn
        map.put("lang","zh-cn");
        model.otcConfig(map)
                .compose(RxUtils.schedulersTransformer()) //线程调度
                .compose(RxUtils.exceptionTransformer()) // 网络错误的异常转换, 这里可以换成自己的ExceptionHandle
                .doOnSubscribe(this)//请求与ViewModel周期同步
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
//                        showDialog("正在请求...");
                    }
                })
                .subscribe(new DisposableObserver<BaseResponse<TimeNumZone>>() {
                    @Override
                    public void onNext(BaseResponse<TimeNumZone> response) {
                        Log.e("TAG","==========requestNetWork======otcConfig===onNext===="+response.getData().num_zone.size());
                        Log.e("TAG","==========requestNetWork======otcConfig===onNext===123="+response.getData().num_zone.get(0).keySet().toString());
//                        //清除列表
//                        observableList.clear();
//                        //请求成功
//                        if (response.getCode() == 1) {
//                            for (DemoEntity.ItemsEntity entity : response.getResult().getItems()) {
//                                TransactionTabItemViewModel itemViewModel = new TransactionTabItemViewModel(TransactionTabViewModel.this, entity);
//                                //双向绑定动态添加Item
//                                observableList.add(itemViewModel);
//                            }
//                        } else {
//                            //code错误时也可以定义Observable回调到View层去处理
//                            ToastUtils.showShort("数据错误");
//                        }
                    }

                    @Override
                    public void onError(Throwable throwable) {
                        Log.e("TAG","==========requestNetWork======otcConfig===onError====");
                        //关闭对话框
//                        dismissDialog();
//                        //请求刷新完成收回
//                        uc.finishRefreshing.call();
//                        if (throwable instanceof ResponseThrowable) {
//                            ToastUtils.showShort(((ResponseThrowable) throwable).message);
//                        }
                    }

                    @Override
                    public void onComplete() {
                        Log.e("TAG","==========requestNetWork======otcConfig===onComplete====");
                        //关闭对话框
//                        dismissDialog();
                        //请求刷新完成收回
//                        uc.finishRefreshing.call();
                    }
                });
    }

    public void planWallet() {
        Log.e("TAG","==========planWallet=============");
        //可以调用addSubscribe()添加Disposable，请求与View周期同步
        HashMap<String, Object> map = new HashMap<>();
//        lang=zh-cn
        map.put("lang","zh-cn");
        model.planWallet(map)
                .compose(RxUtils.schedulersTransformer()) //线程调度
                .compose(RxUtils.exceptionTransformer()) // 网络错误的异常转换, 这里可以换成自己的ExceptionHandle
                .doOnSubscribe(this)//请求与ViewModel周期同步
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
//                        showDialog("正在请求...");
                    }
                })
                .subscribe(new DisposableObserver<BaseResponse<WalletEntity>>() {
                    @Override
                    public void onNext(BaseResponse<WalletEntity> response) {
                        Log.e("TAG","==========planWallet=========onNext===="+response.getData().coin_name);
                    }
                    @Override
                    public void onError(Throwable throwable) {
                        Log.e("TAG","==========planWallet=========onError====");
                    }
                    @Override
                    public void onComplete() {
                        Log.e("TAG","==========planWallet=========onComplete====");
                    }
                });
    }


    public void otcConfig() {
        Log.e("TAG","==========otcConfig=============");
        model.otcConfig()
                .compose(RxUtils.schedulersTransformer()) //线程调度
                .compose(RxUtils.exceptionTransformer()) // 网络错误的异常转换, 这里可以换成自己的ExceptionHandle
                .doOnSubscribe(this)//请求与ViewModel周期同步
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
//                        showDialog("正在请求...");
                    }
                })
                .subscribe(new DisposableObserver<BaseResponse<OtcConfig>>() {
                    @Override
                    public void onNext(BaseResponse<OtcConfig> response) {
                        Log.e("TAG","==========otcConfig=========onNext====");
                        if(null != response && null != response.getData())
                        {
                            Log.e("TAG","==========otcConfig=========onNext==1111==");
                            mOtcConfig.postValue(response.getData());
                        }
                    }
                    @Override
                    public void onError(Throwable throwable) {
                        Log.e("TAG","==========otcConfig=========onError===="+throwable.getMessage());
                        mOtcConfig.postValue(null);
                    }
                    @Override
                    public void onComplete() {
                        Log.e("TAG","==========otcConfig=========onComplete====");
                    }
                });
    }
}
